package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqMenuPrivilege;
import cloud.maque.admin.domain.entity.MqPrivilege;
import cloud.maque.admin.mapper.MqPrivilegeMapper;
import cloud.maque.admin.service.MqMenuPrivilegeService;
import cloud.maque.admin.service.MqPrivilegeService;
import cloud.maque.admin.service.MqRolePrivilegeService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description mq_privilege
 * @date 2023-03-17
 */
@Service
public class MqPrivilegeServiceImpl extends ServiceImpl<MqPrivilegeMapper, MqPrivilege> implements MqPrivilegeService {


    @Autowired
    MqRolePrivilegeService rolePrivilegeService;

    @Autowired
    MqMenuPrivilegeService menuPrivilegeService;

    @Override
    public MqPrivilege savePrivilege(MqPrivilege privilege) {
        Assert.hasText(privilege.getPrivilegeName(), "权限名称不能为空");
        saveOrUpdate(privilege);
        return privilege;
    }

    @Override
    @Transactional
    public Boolean delete(String privilegeId, String tenantId) {

        QueryWrapper<MqPrivilege> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqPrivilege::getTenantId, tenantId);
        wrapper.lambda().eq(MqPrivilege::getPrivilegeId, privilegeId);
        if (remove(wrapper)) {
            //删除与角色关系
            rolePrivilegeService.deleteByPrivilegeId(privilegeId);
            //删除与菜单关系
            menuPrivilegeService.DeleteByPrivilegeId(privilegeId);
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteByMenuId(String menuId) {
        //删除与菜单关系
        menuPrivilegeService.DeleteByMenuId(menuId);
        return removeById(menuId);
    }


    @Override
    public Integer countByGroupId(String groupId) {

        QueryWrapper<MqPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqPrivilege::getPrivilegeGroupId, groupId);
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public PageInfo<MqPrivilege> page(PaginateDataResquest<MqPrivilege> pageReq) {
        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqPrivilege> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getPrivilegeName())) {
            wrapper.lambda().like(MqPrivilege::getPrivilegeName, pageReq.getData().getPrivilegeName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getPrivilegeCode())) {
            wrapper.lambda().like(MqPrivilege::getPrivilegeCode, pageReq.getData().getPrivilegeCode());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getPrivilegeGroupId())) {
            wrapper.lambda().eq(MqPrivilege::getPrivilegeGroupId, pageReq.getData().getPrivilegeGroupId());
        } else {
            wrapper.lambda().isNotNull(MqPrivilege::getPrivilegeGroupId);
            wrapper.lambda().ne(MqPrivilege::getPrivilegeGroupId, "");
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqPrivilege::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqPrivilege::getPrivilegeOrder);
        wrapper.lambda().orderByAsc(MqPrivilege::getPrivilegeId);
        List<MqPrivilege> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqPrivilege> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }

    @Override
    public List<MqPrivilege> allList(MqPrivilege pageReq) {
        QueryWrapper<MqPrivilege> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getPrivilegeGroupId())) {
            wrapper.lambda().eq(MqPrivilege::getPrivilegeGroupId, pageReq.getPrivilegeGroupId());
        } else {
            wrapper.lambda().isNotNull(MqPrivilege::getPrivilegeGroupId);
            wrapper.lambda().ne(MqPrivilege::getPrivilegeGroupId, "");
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getTenantId())) {
            wrapper.lambda().eq(MqPrivilege::getTenantId, pageReq.getTenantId());
        }

        wrapper.lambda().orderByAsc(MqPrivilege::getPrivilegeOrder);
        wrapper.lambda().orderByAsc(MqPrivilege::getPrivilegeId);
        List<MqPrivilege> lists = this.baseMapper.selectList(wrapper);

        return lists;
    }

    @Override
    public MqPrivilege CreateMenuPrivileleg(String menuId, String tenantId) {

        //添加权限和关系，主键和菜单ID一致
        MqPrivilege privilege = new MqPrivilege();
        privilege.setPrivilegeId(menuId);
        privilege.setPrivilegeName("菜单基本权限");
        privilege.setPrivilegeGroupId("");
        privilege.setTenantId(tenantId);

        saveOrUpdate(privilege);

        MqMenuPrivilege mqMenuPrivilege = new MqMenuPrivilege();
        mqMenuPrivilege.setPrivilegeId(menuId);
        mqMenuPrivilege.setMenuId(menuId);

        menuPrivilegeService.saveOrUpdate(mqMenuPrivilege);

        return privilege;

    }
}
